using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Cache;
using System.Text;

namespace mst_regex
{
    internal static class WebDownloader
    {
        public const int DEFAULT_DOWNLOAD_TIMEOUT = 60*1000;

        public static string DownloadHtml(Uri location, bool enableGzipCompression)
        {
            HttpWebRequest request = ConfigureRequest(location, enableGzipCompression);
            string result = string.Empty;

            try
            {
                using (var response = (HttpWebResponse) request.GetResponse())
                {
                    Stream responseStream = response.GetResponseStream();
                    if (response.ContentEncoding.ToLower().Contains("gzip"))
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);

                    using (var Reader = new StreamReader(responseStream, Encoding.Default))
                    {
                        result = Reader.ReadToEnd();
                    }
                }
            }
            catch (WebException webError)
            {
                /*
                string sError = string.Empty;
                if (webError.Response != null)
                {
                    using (StreamReader reader = new StreamReader(webError.Response.GetResponseStream()))
                    {
                        sError = reader.ReadToEnd();
                    }
                }
                throw new Exception(sError);
                 */
            }
            catch
            {
            }
            return result;
        }

        private static HttpWebRequest ConfigureRequest(Uri location, bool GzipEnabled)
        {
            var request = (HttpWebRequest) WebRequest.Create(location);

#if _PROXY_USE
            if (appSettings.UseProxy)
            {
                if (!(string.IsNullOrEmpty(appSettings.ProxyAddress) || (appSettings.ProxyPort == -1)))
                {
                    WebProxy proxy = new WebProxy(appSettings.ProxyAddress, appSettings.ProxyPort)
                                         {
                                             Credentials =
                                                 new NetworkCredential(appSettings.ProxyUsername,
                                                                       appSettings.ProxyPassword),
                                             BypassProxyOnLocal = true
                                         };
                    request.Proxy = proxy;
                }
                else
                {
                    request.Proxy = WebRequest.DefaultWebProxy;
                }
            }
#endif
            var noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            request.UserAgent = GetRandomUserAgent();
            request.Referer = location.AbsoluteUri;
            request.Timeout = DEFAULT_DOWNLOAD_TIMEOUT;
            if (GzipEnabled)
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

            return request;
        }

        private static string GetRandomUserAgent()
        {
            var rand = new Random();

            string[] userAgents = {
                                      @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"
                                      ,
                                      @"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
                                      ,
                                      @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.3 (KHTML, like Gecko) Chrome/4.0.223.16 Safari/532.3"
                                      ,
                                      @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
                                  };

            return userAgents[rand.Next(userAgents.Length)];
        }
    }
}